
(function () {
    CAAT.SceneActor = function () {
        CAAT.SceneActor.superclass.constructor.call(this);
        return this;
    }

    CAAT.SceneActor.prototype = {
        isDim: null,
        director: null,
        nextScene: null,
        prevScene: null,

        initialize: function (director, nextScene, prevScene) {
            this.director = director;
            this.nextScene = nextScene;
            this.prevScene = prevScene;
            return this;
        },

        paint: function(director, time) {
            CAAT.SceneActor.superclass.paint.call(this, director, time);
            var ctx = director.ctx;
            if (this.isDim) {
                ctx.fillStyle = "rgba(0,0,0,0.5)";
                ctx.fillRect(0, 0, this.width, this.height);
            }
        },

        switchToNextScene: function () {
            if (this.nextScene) {
                this.director.switchToScene(this.nextScene);
            }
            return this;
        },

        switchToPrevScene: function () {
            if (this.prevScene) {
                this.director.switchToScene(this.prevScene);
            }
        }
    }
    extend(CAAT.SceneActor, CAAT.ActorContainer);
})();
(function () {
    CAAT.MenuActor = function () {
        CAAT.MenuActor.superclass.constructor.call(this);
        return this;
    }

    var sceneGameContainer;
    CAAT.MenuActor.prototype = {
        buttons: null,

        initialize: function (director, nextScene,sceneGameCtn) {
            CAAT.MenuActor.superclass.initialize.call(this, director, nextScene);
            var self = this;
            sceneGameContainer = sceneGameCtn;
            this.buttons = [];
            this.setBackgroundImage(director.getImage("menu_bg"));
            var buttonImage = new CAAT.SpriteImage().initialize(director.getImage("menu_button"), 4, 2);
            var i = 0;
            for (i; i < 4; i++) {
                var button = new CAAT.Button().initialize(director, buttonImage, 2 * i, 2 * i + 1, 2 * i + 1);
                self.buttons.push(button);
                button.setActionPerformed(self.menuButtonAction(i));
                button.setPosition(550, 250 + i * 50);
                self.addChild(button);
            }
            return this;
        },

        enableButton: function () {
            var buttons = this.buttons;
            for (var i = 0; i < buttons.length; i++) {
                buttons[i].setEnabled(true);
            }
            return this;
        },

        disableButton: function () {
            var buttons = this.buttons;
            for (var i = 0; i < buttons.length; i++) {
                buttons[i].setEnabled(false);
            }
            return this;
        },

        menuButtonAction: function (j) {
            var self = this;
            return function () {
                switch (j) {
                    /* Start */
                    case 0:
                            console.log(sceneGameContainer);
                        sceneGameContainer.reset();
                        self.switchToNextScene();
                        break;
                        /* Unlock */
                    case 1:
                        console.log('unlock');
                        self.isDim = true;
                        self.disableButton();
                        break;
                        /* Option */
                    case 2:
                        console.log('option');
                        self.isDim = true;
                        self.disableButton();
						var option = new CAAT.OptionContainer().initialize(self.director);
						option.setLocation((CANVAS_WIDTH-option.width)/2,100);
						option.setCallbackFunction(function(){
							self.isDim = false;
                            self.enableButton();
							self.removeChild(self.option);
						});
						self.option = option;
						self.addChild(option);
                        break;
                        /* Credits */
                    case 3:
                        console.log('credits');
                        self.isDim = true;
                        self.disableButton();
                        var credits = [
                            "PROGRAM\n" +
                            "       Lê Việt Hà\n" + 
                            "       Nguyễn Thanh Tùng",
                            "GRAPHICS\n" +
                            "       Lê Việt Hà\n" + 
                            "       Nguyễn Thanh Tùng",
                            "CODER\n" +
                            "       Lê Việt Hà\n" + 
                            "       Nguyễn Thanh Tùng",
                            "LOGICTIC\n" +
                            "      Lê Việt Hà\n" + 
                            "      Nguyễn Thanh Tùng",
                            "MUSIC\n" +
                            "       Lê Việt Hà\n" + 
                            "       Nguyễn Thanh Tùng",
                            "       SPECIAL THANK\n\n"+
                            "       Hot girl Ai Shinozaki :v\n\n\n"+
                            "For help us complete this games!",
                            "       Bring to you by BKGM\n\n"+
                            "      THANK FOR PLAYING!"
                        ];
                        var creditActor = new CAAT.CreditsActor().initialize(self.director, credits);
                        creditActor.setPosition((CANVAS_WIDTH-creditActor.width)/2,100);
                        creditActor.setFn(function () {
                            self.isDim = false;
                            self.enableButton();
                            self.removeChild(this);
                        });
                        self.addChild(creditActor);
                        break;
                }
            }
        }
    }
    extend(CAAT.MenuActor, CAAT.SceneActor);
})();

(function () {
    CAAT.CreditsActor = function () {
        CAAT.CreditsActor.superclass.constructor.call(this);
        return this;
    }

    CAAT.CreditsActor.prototype = {
        director: null,
        credits: null,
        currentCredit: null,
        numberCredits: null,
        fn: null,
        initialize: function (director, credits, fn) {
            var self = this;
            this.director = director;
            this.credits = credits;
            this.currentCredit = 0;
            this.numberCredits = credits.length;
            this.fn = fn;
            this.setBackgroundImage(director.getImage("credits_bg"));
            this.sceneTime = 0;
            var fadeBehavior = new CAAT.AlphaBehavior().setValues(0, 1).setFrameTime(this.sceneTime, 500).
                addListener({
                    behaviorExpired: function (behavior, time, actor) {
                        actor.emptyBehaviorList();
                        
                        var textActor = new CAAT.MyTextActor().initialize(director, credits[0], 350);
                        textActor.setTextFillStyle("rgb(255, 223, 167)");
                        textActor.setHorizontalAlign("left");
                        textActor.setFontSize(20);
                        textActor.setLineHeight(25);
                        textActor.setPosition(120, 100);

                        var behavior = new CAAT.AlphaBehavior().setValues(0, 1).setPingPong().setFrameTime(time, 3000).
                            addListener({
                                behaviorExpired: function (behavior, time, actor) {
                                    self.currentCredit++;
                                    if (self.currentCredit < self.numberCredits) {
                                        actor.setText(credits[self.currentCredit]);
                                        behavior.setFrameTime(time, 3000);
                                        actor.addBehavior(behavior);
                                    } else {
                                        self.actionComplete();
                                        actor.emptyBehaviorList();
                                    }
                                }
                            });
                        textActor.addBehavior(behavior);

                        var img = new CAAT.SpriteImage().initialize(director.getImage("cancelButton"), 1, 1);
                        var cancelButton = new CAAT.Button().initialize(director, img, 0, 0, 0, 0, function () {
                            textActor.emptyBehaviorList();
                            self.actionComplete();
                        });
                        cancelButton.setPosition(400, 250);

                        self.addChild(textActor);
                        self.addChild(cancelButton);
                    }
                })
            this.addBehavior(fadeBehavior);

            
            return this;
        },
        paint: function(director,time){
            CAAT.CreditsActor.superclass.paint.call(this, director, time);
            this.sceneTime = time;
        },
        setFn: function(fn) {
            this.fn = fn;
        },

        actionComplete: function () {
            if (this.fn) {
                this.fn();
            }
            return;
        }
    }
    extend(CAAT.CreditsActor, CAAT.ActorContainer);
})();